"""
定义/封装加解密工具类
"""
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

# 封装
class SecretOauth(object):

    def __init__(self):
        # 实例属性
        self.serializer = Serializer(
            secret_key=settings.SECRET_KEY,
            expires_in=3600*24*14 # 默认3600秒
        )

    # 加密
    def dumps(self, content_dict):
        # content_dict: 被加密的原数据
        result = self.serializer.dumps(content_dict)
        return result.decode() # 返回字符串密文

    # 解密
    def loads(self, data):
        # data: 密文数据
        try:
            result = self.serializer.loads(data) # result是解密后的原数据
        except Exception as e:
            print(e)
            return None # 如果解密失败，约定返回None

        return result







